﻿Imports ControllerSigeco

Public Class frmRubro

    Private Sub frmRubro_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgRubros.AutoGenerateColumns = False
        CargarRubros()
    End Sub

    Dim classController As New controller_stock

    Private _idRubro As Integer = 0

    Public Property idRubro() As Integer
        Get
            Return _idRubro
        End Get
        Set(value As Integer)
            _idRubro = value
        End Set
    End Property

    Private Sub CargarRubro()
        Try
            If idRubro > 0 Then

                Dim ds As New DataTable
                ds = classController.get_rubros(oiml.dbConn, idRubro, "")

                If Not ds Is Nothing Then

                    txtRubro.Text = ds.Rows(0)("marca")
                    btnGrabar.Enabled = True
                    btnEliminar.Enabled = True
                    btnBuscar.Enabled = False

                End If

            Else
                txtRubro.Text = ""
                txtRubro.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub CargarRubros()
        Try
            idRubro = 0
            txtRubro.Focus()

            btnBuscar.Enabled = True
            btnNuevo.Enabled = True
            btnEliminar.Enabled = True
            btnGrabar.Enabled = False

            Dim ds As New DataTable
            ds = classController.get_rubros(oiml.dbConn, 0, txtRubro.Text)

            If Not ds Is Nothing Then
                dgRubros.DataSource = ds
            Else
                dgRubros.DataSource = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click

        If idRubro > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Rubros", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_rubro(oiml.dbConn, idRubro) Then
                    txtRubro.Text = ""
                    CargarRubros()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Rubros", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtRubro.Text = "" Then
            ErrorProvider1.SetError(txtRubro, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtRubro, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnGrabar_Click(sender As System.Object, e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If idRubro = 0 Then
            idRubro = classController.Insert_rubro(oiml.dbConn, txtRubro.Text)
            If idRubro > 0 Then
                txtRubro.Text = ""
                CargarRubros()
                MessageBox.Show("Registro grabado con exito!", "Rubros", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Rubros", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_rubro(oiml.dbConn, idRubro, txtRubro.Text) Then
                txtRubro.Text = ""
                CargarRubros()
                MessageBox.Show("Registro grabado con exito!", "Rubros", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)


            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Rubros", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub editar()
        Try

            If dgRubros.SelectedRows.Count > 0 Then

                idRubro = dgRubros.SelectedRows(0).Cells(0).Value
                CargarRubro()

            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        btnEliminar.Enabled = False
        btnNuevo.Enabled = False
        btnBuscar.Enabled = False
        btnGrabar.Enabled = True

        idRubro = 0
        txtRubro.Text = ""
        txtRubro.Focus()

    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        CargarRubros()

    End Sub

    Private Sub dgRubros_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgRubros.KeyDown
        If e.KeyCode = Keys.Enter Then
            editar()
        End If
    End Sub

    Private Sub dgRubros_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgRubros.DoubleClick
        editar()
    End Sub

    Private Sub txtMarca_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtRubro.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarRubros()
        End If
    End Sub

    Private Sub dgRubros_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgRubros.CellClick
        Try

            If dgRubros.SelectedRows.Count > 0 Then

                idRubro = dgRubros.SelectedRows(0).Cells(0).Value


            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Class